package com.zcxy360.sandtable.controller;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zcxy360.sandtable.domain.Org;
import com.zcxy360.sandtable.service.OrgService;

/**
 * 机构接口
 * @author 盛亚辉
 *
 */
@RequestMapping("org")
@RestController
public class OrgController {
	
	@Autowired
	private OrgService orgService;

	/**
	 * 获取机构
	 * @param orgId 机构id
	 * @return 机构对象
	 */
	@RequiresAuthentication
	@GetMapping("{orgId}")
	public Object getByOrgId(@PathVariable("orgId") Integer orgId) {
		return orgService.getByOrgId(orgId);
	}
	
	/**
	 * 获取机构集合
	 * @param org 机构字段
	 * @param key 关键字
	 * @param orderByClause 排序规则
	 * @param pageNum 页码
	 * @param pageSize 页数
	 * @param navigatePages 导航数
	 * @return 机构集合
	 */
	@RequiresRoles("admin")
	@GetMapping
	public Object list(Org org,String key,String orderByClause,
			@RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "5") Integer pageSize,
			@RequestParam(defaultValue = "5") Integer navigatePages) {
		return orgService.list(org,key,orderByClause,pageNum,pageSize,navigatePages);
	}
	
	/**
	 * 新增机构
	 * @param org 机构字段
	 * @return 机构对象
	 */
	@RequiresRoles("admin")
	@PostMapping
	public Object post(Org org) {
		return orgService.post(org);
	}
	
	/**
	 *  更新机构
	 * @param org 机构字段
	 * @return 布尔值
	 */
	@RequiresRoles("admin")
	@PutMapping
	public Object update(Org org) {
		return orgService.update(org);
	}
	
	/**
	 * 删除机构
	 * @param orgIds 机构id数组
	 * @return 布尔值
	 */
	@RequiresRoles("admin")
	@DeleteMapping("{orgIds}")
	public Object delete(@PathVariable("orgIds")int[] orgIds) {
		return orgService.delete(orgIds);
	}
	
	/**
	 * 检查名称
	 * @param name 机构名称
	 * @return 布尔值
	 */
	@RequiresRoles("admin")
	@GetMapping("checkName")
	public Object checkName(@RequestParam("name")String name) {
		return orgService.checkName(name);
	}
	
}
